﻿Imports System
Imports System.Collections
Imports System.ComponentModel

Namespace System.Workflow.ComponentModel.Serialization
    <ContentProperty("Items")> _
    Friend NotInheritable Class ArrayExtension
        Inherits MarkupExtension
        ' Methods
        Public Sub New()
            Me.arrayElementList = New ArrayList
        End Sub

        Public Sub New(ByVal elements As Array)
            Me.arrayElementList = New ArrayList
            If (elements Is Nothing) Then
                Throw New ArgumentNullException("elements")
            End If
            Me.arrayElementList.AddRange(elements)
            Me.arrayType = elements.GetType.GetElementType
        End Sub

        Public Sub New(ByVal arrayType As Type)
            Me.arrayElementList = New ArrayList
            If (arrayType Is Nothing) Then
                Throw New ArgumentNullException("arrayType")
            End If
            Me.arrayType = arrayType
        End Sub

        Public Overrides Function ProvideValue(ByVal provider As IServiceProvider) As Object
            If (provider Is Nothing) Then
                Throw New ArgumentNullException("provider")
            End If
            If (Me.arrayType Is Nothing) Then
                Throw New InvalidOperationException("ArrayType needs to be set.")
            End If
            Dim obj2 As Object = Nothing
            Try 
                obj2 = Me.arrayElementList.ToArray(Me.arrayType)
            Catch exception1 As InvalidCastException
                Throw New InvalidOperationException
            End Try
            Return obj2
        End Function


        ' Properties
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public ReadOnly Property Items As IList
            Get
                Return Me.arrayElementList
            End Get
        End Property

        Public Property Type As Type
            Get
                Return Me.arrayType
            End Get
            Set(ByVal value As Type)
                Me.arrayType = value
            End Set
        End Property


        ' Fields
        Private arrayElementList As ArrayList
        Private arrayType As Type
    End Class
End Namespace

